<template>
  <div class="role">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div>
          <span>筛选搜索</span>
          <el-button style="float: right;" type="primary" @click="handleSearch">查询搜索</el-button>
          <el-button style="float: right;margin-right: 15px" @click="reset">重置</el-button>
        </div>
        <div style="margin-top: 15px">
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item label="输入搜索：">
              <el-input v-model="form.name" placeholder="角色名称"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-card shadow="never">
        <el-button @click="handleAdd">添加角色</el-button>
      </el-card>
      <el-table
        v-loading="loading"
        :data="roleList"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          align="center"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名称"
          align="center"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          align="center"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="添加时间"
          align="center"
          min-width="150">
          <template slot-scope="scope">
            <span>{{scope.row.createdTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          min-width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleAllotMenu(scope.row)">分配菜单</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleAllotResource(scope.row)">分配资源</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="handleTitle" :visible.sync="visible" width="30%">
      <el-form :model="handleForm">
        <el-form-item label="角色名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="handleForm.name" autocomplete="off" :style="inputWidth"></el-input>
        </el-form-item>
        <el-form-item label="角色编码" prop="name" :label-width="formLabelWidth">
          <el-input v-model="handleForm.code" autocomplete="off" :style="inputWidth"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="handleForm.description" autocomplete="off" :style="inputWidth"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" :disabled="loading"  @click="saveOrUpdateRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getRolePages, saveOrUpdate, deleteRole } from '@/services/role'

export default Vue.extend({
  name: 'RoleIndex',
  data () {
    return {
      isEdit: false,
      form: {
        name: ''
      },
      loading: false,
      roleList: [],
      formLabelWidth: '120px',
      inputWidth: {
        width: '250px'
      },
      handleForm: {
        name: '',
        code: '',
        status: 1,
        description: ''
      },
      visible: false
    }
  },
  created () {
    this.loadAllRole()
  },
  computed: {
    handleTitle () {
      return (this as any).isEdit ? '编辑角色' : '新增角色'
    }
  },
  methods: {
    async loadAllRole () {
      this.loading = true
      const { data } = await getRolePages({
        current: 1,
        name: this.form.name,
        size: 50
      })
      this.loading = false
      this.roleList = data.data.records
    },
    handleSearch () {
      this.loadAllRole()
    },
    reset () {
      this.form.name = ''
      this.loadAllRole()
    },
    handleAllotMenu (row: any) {
      this.$router.push({
        name: 'role-alloc-menu',
        params: {
          roleId: row.id
        }
      })
    },
    handleAllotResource (row: any) {
      this.$router.push({
        name: 'role-alloc-resource',
        params: {
          roleId: row.id
        }
      })
    },
    handleEdit (row: any) {
      this.isEdit = true
      const item = {
        code: row.code,
        description: row.description,
        id: row.id,
        name: row.name
      }
      this.handleForm = item as any
      this.visible = true
    },
    handleDelete (row: any) {
      this.$confirm('是否删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteRole(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(console.log)
    },
    async saveOrUpdateRole () {
      this.loading = true
      await saveOrUpdate(this.handleForm)
      this.$message.success(`${this.isEdit ? '修改成功' : '新增成功'}`)
      this.loading = false
      this.visible = false
      this.loadAllRole()
    },
    handleAdd () {
      this.isEdit = false
      this.handleForm = {
        name: '',
        code: '',
        status: 1,
        description: ''
      }
      this.visible = true
    }
  }
})
</script>

<style lang="scss" scoped></style>
